//
//  AccountManagerVC.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/5/6.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit
import SVProgressHUD

class AccountManagerVC: RootVC {

    private let datas = [("User Name","AccountManagerVC"),
                         ("Password","SimPinManagerVC")]
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
        label.right = screenWidth - 30
        label.backgroundColor = clearColor
        label.text = UserInfoManager.shared().account
        label.textAlignment = .right
        return label
    }()
    
    private lazy var passwordLabel: UILabel = {
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
        label.right = screenWidth - 30
        label.backgroundColor = clearColor
        label.text = "*****"
        label.textAlignment = .right
        return label
    }()
    
    private lazy var tableView: UITableView = {
        let tab = UITableView(frame: CGRect(x: 0, y: naviView!.bottom, width: screenWidth, height: screenHeight - naviView!.bottom), style: .grouped)
        tab.backgroundColor = whiteColor
        tab.rowHeight = 50
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = .none
        tab.isScrollEnabled = false
        return tab
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        NotificationCenter.default.addObserver(self, selector: #selector(reloadDate), name: NSNotification.Name(rawValue: "CloseCustomAlertVC"), object: nil)
  

    }
    
    @objc private func reloadDate(){
    
        nameLabel.text = UserInfoManager.shared().account
    
    }
    
    
    override func configView() {
        configNaviView(title: "Account Manager")
        view.addSubview(tableView)
    }
    
    private func showAccountAlert() {
        let alertView = InputAlertView(input: nameLabel.text!)
        alertView.delayDismiss = true
        let alertVC = CustomAlertVC(contentType: .animation(alertView))
        alertVC.show()
    }
    
    private func showPasswordAlert() {
        let alertView = TwoInputsAlertView(placeholder: "")
        let alertVC = CustomAlertVC(contentType: .animation(alertView))
        alertVC.show()
    }
}

extension AccountManagerVC : UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = datas[indexPath.row].0
        cell.textLabel?.font = UIFont.systemFont(ofSize: 18)
        cell.textLabel?.textColor = textGray
        cell.selectionStyle = .none
        cell.accessoryType = .disclosureIndicator
        
        nameLabel.font = cell.textLabel?.font
        nameLabel.textColor = cell.textLabel?.textColor
        passwordLabel.font = cell.textLabel?.font
        passwordLabel.textColor = cell.textLabel?.textColor
        
        if indexPath.row == 0 {
            cell.contentView.addSubview(nameLabel)
        }else {
            cell.contentView.addSubview(passwordLabel)
        }
        
        let line = UIView(frame: CGRect(x: 8, y: 50 - 0.5, width: screenWidth - 16, height: 0.5))
        line.backgroundColor = textGray
        cell.contentView.addSubview(line)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            showAccountAlert()
        }else {
            showPasswordAlert()
        }
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
